package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.common.MessageConst;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* @Author : wangjie
* @CreateDate : 2020/4/3 10:56
* @Version : 1.0
* @Description : 预约设置前端控制器
*/
@RestController
@RequestMapping("ordersetting")
@Slf4j
public class OrderSettingController {

    //依赖注入service
    @Reference
    private OrderSettingService orderSettingService;
    /**
     * 上传文件
     * @param excelFile
     * @return
     */
    @RequestMapping("upload")
    public Result update(@RequestParam("excelFile") MultipartFile excelFile){
        log.debug("upload:{}"+excelFile.getOriginalFilename());
        try {
            //利用POI工具类,从文件附件项中获取Excel原始数据( List<String[]> )
            List<String[]> list = POIUtils.readExcel(excelFile);
            if (list!=null) {
                //把List<String[]>数组列表, 转换为List<OrderSetting>对象列表
                List<OrderSetting> orderSettingList = new ArrayList<>();
                //从excel遍历数据,封装数据为设置对象,添加到集合
                for (String[] row : list) {
                    if(row[0] == null){
                        continue;
                    }
                    OrderSetting orderSetting = new OrderSetting(new Date(row[0]), Integer.parseInt(row[1]));
                    orderSettingList.add(orderSetting);
                }
                //调用Service, 批量完成OrderSetting数据的保存
                orderSettingService.upload(orderSettingList);
                //保存成功,返回成功消息
                return new Result(true, MessageConst.IMPORT_ORDERSETTING_SUCCESS);
            } else {
                throw new RuntimeException("数据不能为空");
            }
        } catch (IOException e) {
            e.printStackTrace();
            //保存失败,返回异常信息
            return new Result(false, MessageConst.IMPORT_ORDERSETTING_FAIL);
        }
    }

    /**
     * 获取制定月的预约数据
     * @return date(月份)
     */
    @RequestMapping("getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date){
        try {
            //调用service 方法 获取预约数据
            List<Map<String,Integer>> list= orderSettingService.getOrderSettingByMonth(date);
            return new Result(true,MessageConst.GET_ORDERSETTING_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.GET_ORDERSETTING_FAIL);
        }
    }

    /**
     * 根据日期 修改预约设置数据
     * @param orderSetting
     * @return
     */
    @RequestMapping("editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        try {
            //调用service 方法 编辑数据
            orderSettingService.editNumberByDate(orderSetting);
            //返回成功信息
            return new Result(true,MessageConst.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.ORDERSETTING_FAIL);
        }
    }
}
